<template>
    <div class="app-container">
        <el-card shadow="never">
            <!-- 条件查询 -->
            <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="120px" @keyup.enter.native="submitSearch" @submit.native.prevent>
                <el-row>

                    <el-col :md="6" :sm="12">
                        <el-form-item class="ele-text-right" label-width="50px">
                            <el-button type="primary" size="mini" @click="submitSearch" icon="el-icon-search" class="ele-btn-icon">搜索 </el-button>
                            <el-button @click="resetQuery" size="mini" icon="el-icon-refresh">重置</el-button>

                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>

            
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <!-- <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="open" v-hasPermi="api.add">添加</el-button> -->
                </el-col>
                <el-col :span="1.5">
                    <!-- <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="api.delete">删除</el-button> -->
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" size="mini" v-loading="exportLoading" @click="handleExport" v-hasPermi="api.export">导出</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-upload
                        class="upload-demo"
                        :data="uploadData"
                        ref="upload"
                        :action="api.upload"

                        :limit="1"
                        :multiple="false"
                        
                        :on-preview="handlePreview"
                        :on-remove="handleRemove"
                        :on-success="handleUploadSuccess"
                        :on-error="handleUploadError"
                        :auto-upload="false">
                        <el-button v-hasPermi="api.upload" slot="trigger" size="small" type="primary">选择文件</el-button>
                        <el-button style="margin-left: 10px;" size="small" type="info" @click="submitUpload()" v-hasPermi="api.upload">上传交易记录</el-button>
                        <!-- <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div> -->
                    </el-upload>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="initIndex" :columns="columns" printId="#quickadmin"></right-toolbar>
            </el-row>

            <!-- 列表 -->
            <qa-table
                :source="tableData"
                :columns="columns"
                :selection="true"
                :loading="loading"
                @sortChange="sortChange"
                @statusChange="statusChange"
                @selectionChange="selectionChange"
                id="quickadmin"
            >
                <template v-slot:action>
                    <el-table-column label="操作" width="240" align="left">
                        <template slot-scope="scope">
                            <el-button class="btn-mar" size="mini" type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)" v-hasPermi="api.edit">编辑</el-button>
                            <!-- <el-button class="btn-mar" size="mini" type="info" icon="el-icon-tickets" @click="handleDetail(scope.row)" v-hasPermi="api.find">详情</el-button> -->
                            <!-- <el-button class="btn-mar" size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="api.delete">删除</el-button> -->

                            <el-button class="btn-mar" size="mini" type="warning" @click="jiesuan(scope.row)">结算</el-button>

                        </template>
                    </el-table-column>
                </template>
            </qa-table>

            <!-- 分页 -->
            <pagination :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="initIndex" />
        </el-card>

        <!-- 编辑弹窗 -->
       <el-dialog :title="!disabled ? '编辑' : '详情'" :visible="editOpen"  :before-close="beforeClose" destroy-on-close>
            <EditForm ref="editForm" :findFormData="findFormData"  :disabled="disabled" @submitForm="submitForm" :editFormParams="editFormParams" @editFormCancel="beforeClose" />
        </el-dialog>

    </div>
</template>

<script>
import { curdMixin } from '@/mixins/curdMixin'
import EditForm from './EditFormCom'
export default {
    name: 'Curd',
    mixins: [curdMixin],
    components: { EditForm },
    
    data() {
        return {
            // table结构
            columns: [
                {
                    visible: true,
                    label: '运营',
                    prop: 'nickname',
                },
                {
                    visible: true,
                    label: '店铺',
                    prop: 'site',
                },
                {
                    visible: true,
                    label: '销售额',
                    prop: 'sale',
                },
                {
                    visible: true,
                    label: '亚马逊其他',
                    prop: 'amz',
                },
                {
                    visible: true,
                    label: '平台费',
                    prop: 'plat',
                },
                {
                    visible: true,
                    label: '广告费',
                    prop: 'adv',
                },
                {
                    visible: true,
                    label: '商品成本',
                    prop: 'product_cost',
                },
                {
                    visible: true,
                    label: '物流费用',
                    prop: 'logistics',
                },
                {
                    visible: true,
                    label: '其他消费',
                    prop: 'other',
                },
                {
                    visible: true,
                    label: '利润',
                    prop: 'profit',
                },
                // {
                //     visible: true,
                //     label: '提成',
                //     prop: 'reduce',
                // },
                // {
                //     visible: true,
                //     label: '奖金',
                //     prop: 'bonus',
                // },
                // {
                //     visible: true,
                //     label: '组长提成',
                //     prop: 'group_reduce',
                // },
                {
                    visible: true,
                    label: '结算时间',
                    prop: 'createtime',
                },

            ],
            // 接口地址
            api: {
                index: `/admin/yeji/index`,
                find: `/admin/yeji/find`,
                add: `/admin/yeji/add`,
                edit: `/admin/yeji/edit`,
                delete: `/admin/yeji/delete`,
                export: `/admin/yeji/export`,
                upload: `https://api.happyamz.com/admin/translate/uploads`,
                jiesuan: `/admin/yeji/jiesuan`,
                status: `/admin/yeji/status`
            },
            editFormParams: {
            },
            pageInfo: {
                sort: 'id',
            },
            primaryKey: 'id',
            // 搜索表单是否展开
            searchExpand: false,
              //是否展示搜索
            showSearch: false,
            // 查询参数
            queryParams: {

            },
            uploadData:{
                adminid:1,
            },
        }
    },
    created () {
        this.initIndex()
    },
    watch: {
        
    },
    methods: {
        // 案列：当前组件格式化方法
        testFormatter(prop) {
            return prop
        },

        jiesuan(row) {
            this.baseRequest_jiesuan(row)
        },
        baseRequest_jiesuan(row) {
            this.loading = true
            this.request
                .post(this.api.jiesuan, { id: row.id , createtime:row.createtime})
                .then(response => {
                    // if(response == 1){
                        this.$message.success('操作成功');
                    // }
                    // if(response == 2){
                    //     this.$message.error('网络错误，请刷新重试');
                    // }
                    // if(response == 3){
                    //     this.$message.error('已归档');
                    // }
                    this.initIndex();
                })
                .finally(() => {
                    this.loading = false
                    this.initIndex();
                })
        },
        
        submitUpload(row) {
            if(this.ids.length == 1){
                this.loading = true
                this.uploadData.adminid = this.ids[0]
                this.$refs.upload.submit();
            }else{
                this.$message.error('请勾选一个运营再上传交易记录');
            }
        },
        handleRemove(file, fileList) {
            console.log(file, fileList);
        },
        handlePreview(file) {
            console.log(file);
        },
        
        // 上传成功回调
        handleUploadSuccess(res) {
            this.loading = false
            if (res.code == 1) {
                this.$message.success('上传成功');
                this.initIndex();
            } else {
                this.$message({
                    type: 'error',
                    message: '上传失败'+res.msg,
                })
            }
        },
        // 上传失败
        handleUploadError() {
            this.$message({
                type: 'error',
                message: '上传失败',
            })
        },

        // 编辑
        handleEdit (row) {
            this.handleFind(row)
        },

        // 编辑回显
        async handleFind (row) {
            let response = await this.request.get(this.api.edit, { params: { id:row['id'],create_time:row['createtime'] } })
            this.findFormData = this.formatFindData(response.data)
            this.editOpen = true
        },
    }
}
</script>
<style lang="scss">
.btn-mar {
    margin: 5px !important;
}
</style>
